package com.tanhua.manage.controller;

import com.tanhua.manage.service.LogsService;
import com.tanhua.manage.service.ManageUsersService;
import com.tanhua.manage.service.ManageVideoService;
import com.tanhua.manage.vo.ManageUserInfoVo;
import com.tanhua.manage.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("management/manage")
@RestController
public class ManageController {

    @Autowired
    private ManageUsersService manageUsersService;

    @Autowired
    private ManageVideoService manageVideoService;

    @Autowired
    private LogsService logsService;

    //查询用户列表
    @GetMapping("/users")
    public ResponseEntity<PageResult> queryManageUsersList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                           @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                           @RequestParam(value = "id", required = false) Integer id,
                                                           @RequestParam(value = "nickname", required = false) String nickname,
                                                           @RequestParam(value = "city", required = false) String city,
                                                           @RequestHeader(value = "Authorization") String token) {
        try {
            PageResult pageResult = this.manageUsersService.queryManageUsersList(page, pageSize, id, nickname, city, token);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询用户详情
     *
     * @param token
     * @param userID
     * @return
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity<ManageUserInfoVo> queryManageUsersInfo(@RequestHeader(value = "Authorization") String token,
                                                                 @PathVariable(value = "userID") Integer userID) {

        try {
            ManageUserInfoVo manageUserInfoVo = this.manageUsersService.queryManageUsersInfo(token, userID);
            if (manageUserInfoVo != null) {
                return ResponseEntity.ok(manageUserInfoVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频分页
     *
     * @param pagesize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param uid
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity<PageResult> queryManageVideoListById(@RequestParam(name = "pagesize") Integer pagesize,
                                                               @RequestParam(name = "page") Integer page,
                                                               @RequestParam(name = "sortProp") String sortProp,
                                                               @RequestParam(name = "sortOrder") String sortOrder,
                                                               @RequestParam(name = "uid") Integer uid) {
        try {
            //通过传入的参数查询用户的视频列表
            PageResult pageResult = this.manageVideoService.queryManageVideoListById(uid, page, pagesize, sortProp, sortOrder);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 日志分页
     *
     * @param page
     * @param pageSize
     * @param uid
     * @param sortProp
     * @param sortOrder
     * @return
     */
    @GetMapping("/logs")
    private ResponseEntity<PageResult> getLogs(@RequestParam(name = "page") Integer page,
                                               @RequestParam(value = "pagesize") Integer pageSize,
                                               @RequestParam(name = "uid") Integer uid,
                                               @RequestParam(name = "sortProp") String sortProp,
                                               @RequestParam(name = "sortOrder") String sortOrder) {

        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.logsService.getLogs(page, pageSize, uid, sortProp, sortOrder);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户冻结
     *
     * @param
     * @param token
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity<String> freeze(@RequestHeader(value = "Authorization") String token,
                                         @RequestParam Integer userId,
                                         @RequestParam String reasons,
                                         @RequestParam String remarks,
                                         @RequestParam String freezingTime,
                                         @RequestParam  String freezingRange) {
        try {
            boolean b = this.manageUsersService.freeze(token,userId,reasons, remarks,freezingTime, freezingRange);
            if (b) {
                return ResponseEntity.ok("冻结成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户解冻
     *
     * @param param
     * @param token
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity<String> unfreeze(@RequestBody Map<String, Object> param,
                                           @RequestHeader(value = "Authorization") String token) {
        try {
            boolean b = this.manageUsersService.unfreeze(token, param);
            if (b) {
                return ResponseEntity.ok("解冻成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
